import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ganbo
 * @date 2022/12/2 13:47
 */

@Slf4j
public class CServer {

    public static void main(String[] args) {


        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {

                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));  //完善netty日志打印
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast("h1", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("h1");
                                super.channelRead(ctx, msg);

                                System.out.println("h1  after");
                            }
                        });

                        ch.pipeline().addLast("h2", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("h2");
                                super.channelRead(ctx, msg);

                                System.out.println("h2  after");
                            }
                        });

                        ch.pipeline().addLast("h3", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("h3");

                                ctx.channel().writeAndFlush("hello world");
                                super.channelRead(ctx, msg);

                                System.out.println("h3  after");
                            }
                        });

                        ch.pipeline().addLast("h4", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("h4");
                                super.write(ctx, msg, promise);

                                System.out.println("h4  after");
                            }
                        });
                        ch.pipeline().addLast("h5", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("h5");
                                super.write(ctx, msg, promise);

                                System.out.println("h5  after");
                            }
                        });
                        ch.pipeline().addLast("h6", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("h6");
                                super.write(ctx, msg, promise);

                                System.out.println("h6  after");
                            }
                        });
                    }
                })
                .bind(8888);
    }
}
